#include "app/app_runner.h"
#include <stdio.h>
#include "app/app_serial.h"
#include "app/app_pool.h"
#include "app/app_bluetooth.h"
#include "app/app_router.h"
#include "thirdparty/log.h"
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

//串口设备名
#define SERIAL_DEVICE "/dev/ttyS1"
//串口
static SerialDevice serial_device;
//程序运行 1   当外部发送一个信号 变0 停止
static int running = 1;

void signal_handler(int agv)
{
    log_info("Received signal %d", signal);
    running = 0;
}


/// @brief app的主程序
/// @param  
void app_runner_run(void)
{
    signal(SIGINT,signal_handler);  //注册一个信号  
    signal(SIGTERM,signal_handler); //也是停止信号

    //启动线程池  创建5个子线程
    if(Thread_Array_Init(5) < 0)
    {
        log_warn("thread_array init fail");
        exit(EXIT_FAILURE);
    }

    //创建一个设备
    if(app_serial_init(&serial_device,SERIAL_DEVICE) < 0)
    {
        log_warn("app_serial init fail");
        exit(EXIT_FAILURE);
    }

    //初始化路由器
    if(app_touter_init() < 0)
    {
        log_warn("app_touter init fail");
        exit(EXIT_FAILURE);
    }

    //初始化蓝牙
    if(app_bluetooth_setConnectionType(&serial_device) < 0)
    {
        log_warn("app_bluetooth init fail");
        exit(EXIT_FAILURE);
    }

    //注册一个设备 并且启动
    if(app_touter_registerDevice((Device *)&serial_device) < 0)
    {
        log_warn("app_touter register Device fail");
        exit(EXIT_FAILURE);
    }

    while(running)
    {
       sleep(10);
    }

    //删除设备
    app_touter_close();
    //关闭线程池
    Thread_close();
}
